package com.wholesale;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wholesale.beans.GoodsGallery;
import com.wholesale.commons.MyViewGroup;
import com.wholesale.commons.MyViewGroup.OnClickCallback;
import com.wholesale.commons.PageControlView;

public class ShowPhotoLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private PageControlView pageControl;

	private MyViewGroup myGrallyView;

	// 构造函数
	public ShowPhotoLayout(Context context, final ViewFlipper viewFlipper,
			GoodsGallery result, final int selectId) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.showphoto, this);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText("Detail");
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText("Photos");
		myGrallyView = (MyViewGroup) baseView.findViewById(R.id.photoGrally1);

		// List<Drawable> imageList = result.getImageDrawable();
		// if(imageList !=null){
		// for(Drawable drawable : imageList){
		// ImageView imageView = new ImageView(context);
		// imageView.setBackgroundDrawable(drawable);
		// myGrallyView.addView(imageView);
		// }
		// }
		// View temp = baseView.findViewById(R.id.body);
		// int width = temp.getWidth();
		// int height = temp.getHeight();
		// List<Bitmap> imageList = result.getBitmapList();
		// float scaleWidth=0;
		// float scaleHeight=0;
		// if(imageList !=null){
		// for(Bitmap bitmap : imageList){
		// ImageView imageView = new ImageView(context);
		// if(bitmap!=null){
		// scaleWidth = ((float) width) / bitmap.getWidth();
		// scaleHeight = ((float) height) / bitmap.getHeight();
		// Matrix matrix = new Matrix();
		// scaleWidth = scaleWidth>scaleHeight?scaleWidth:scaleHeight ;
		// int newW = (int)(bitmap.getWidth()*scaleWidth)+1;
		// int newH = (int)( bitmap.getHeight()*scaleWidth)+1;
		// matrix.setScale(scaleWidth, scaleWidth);
		// imageView.setImageBitmap(Bitmap.createBitmap(bitmap, 0, 0, newW,
		// newH, matrix, true));
		// }
		// myGrallyView.addView(imageView);
		// }
		// }
		// List<Bitmap> imageList = result.getBitmapList();
		// if(imageList !=null){
		// for(Bitmap bitmap : imageList){
		// ImageView imageView = new ImageView(context);
		// imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
		// if(bitmap!=null){
		// imageView.setImageBitmap(bitmap);
		// }
		// myGrallyView.addView(imageView);
		// }
		// }
		DisplayMetrics dm = new DisplayMetrics();
		((Activity) getContext()).getWindowManager().getDefaultDisplay()
				.getMetrics(dm);
		int width = (int) (dm.widthPixels * 0.7);
		int height = (int) (dm.heightPixels * 0.7);
		myGrallyView.setMinimumHeight(height);
		myGrallyView.setMinimumWidth(width);
		List<Drawable> imageList = result.getImageDrawable();
		if (imageList != null) {
			for (Drawable drawable : imageList) {
				ImageView imageView = new ImageView(context);
				imageView.setMinimumHeight(height);
				imageView.setMinimumWidth(width);
				imageView.setMaxHeight(height);
				imageView.setMaxWidth(width);
				imageView.setImageDrawable(drawable);
				imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
				myGrallyView.addView(imageView);

			}
		}

		myGrallyView.postInvalidate();
		pageControl = (PageControlView) findViewById(R.id.pageControl1);
		pageControl.setCount(myGrallyView.getChildCount());
		pageControl.generatePageControl(0);
		myGrallyView.setScrollToScreenCallback(pageControl);
		myGrallyView.setOnClickCallback(new OnClickCallback() {
			public void callback(int currentIndex) {
				int count = viewFlipper.getChildCount();
				View preView = viewFlipper.getChildAt(count - 2);
				if (preView.getClass().getName()
						.equals(CheckOutLayout.class.getName())) {
					((GoodsDetailLayout) preView)
							.setGrallyPostion(currentIndex);
				}
				viewFlipper.showPrevious();
				viewFlipper.removeView(ShowPhotoLayout.this);
			}
		});
		// Thread t = new Thread(){@Override
		// public void run() {
		// myGrallyView.scrollToScreen(selectId);
		// }};
		// t.run();
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:

			break;
		default:
			break;
		}

	}
}
